<?php

/**
 * Flickr Photosets - Collection Page
 * Displays a list of sets in a collection.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets and Flickr
*/
$fp = new FP_Canvas();

// If the user has disabled photosets
if (!$fp->config['enable_gallery_collections']) {
	if ($fp->config['enable_gallery_photosets']) {
		$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/sets/');
	}
	else if ($fp->config['enable_gallery_photostream']) {
		$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/');
	}
	else if ($fp->config['enable_gallery_geotagged']) {
		$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/geo/');
	}
	else {
		$fp->error(FP_Error::$collections_disabled);
	}
}

// Require that we have the collection id passed in the url
$fp->require_variables($_GET['cid']);

// Get the collection and count
$collections = $fp->flickr->call('flickr.collections.getTree', array('auth_token' => $fp->config['flickr_token']));

$collection = null;
foreach ($collections['collections']['collection'] as $col) {
	if ($col['id'] == $_GET['cid']) {
		$collection = $col;
		break;
	}
}
$collection['flickr_id'] = substr($collection['id'], strpos($collection['id'], '-') + 1);
$photoset_count = count($collection['set']);

// Slice the array into the sets that will be displayed on this page only
$collection['set'] = array_slice($collection['set'], (($fp->page - 1) * $fp->sets_per_page), $fp->sets_per_page);

// Calculate the number of pages we will have
$page_count = ceil($photoset_count / $fp->sets_per_page);

// Get photosets
$sets = $fp->flickr->photosets_getList($fp->nsid);

$photosets = array();
foreach ($sets['photoset'] as $set) {
	$photosets[$set['id']] = $set;
}

?>

<fb:title><?=$fp->get_name_fbml(true)?> Collections | <?=$collection['title']?></fb:title>
<?=$fp->get_stylesheet('collections')?>

<?=$fp->get_header('collections', $collection['title'])?>

<?php if ($photoset_count < 1) {
	$fp->error(FP_Error::$no_collections);
} else { ?>
	<div id="photo_header" class="clearfix">
		<div class="summary">
			<?=$photoset_count?> <fb:intl>Photosets</fb:intl>
			<span class="pipe">|</span>
			<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>collections/<?=$collection['flickr_id']?>/">View collection on Flickr</a>
		</div>
		<?=Pager::getPager($fp->page, $page_count, $fp->canvas_url.'/photos/'.$fp->uid.'/collections/'.$collection['id'].'/')?>
	</div>
	
	<div class="set_grid">
		
		<?php // Loop through each set
		foreach ($collection['set'] as $set): $photoset = $photosets[$set['id']]; ?>
		
		<div class="set clearfix">
			
			<? if ($photoset == null): ?>

			<div class="image">
				<img src="<?=$collection['iconlarge']?>" />
			</div>
			<div class="info">
				<h2><?=$set['title']?></h2>
				<p class="count">This set is not visible due to privacy settings.</p>
				<p class="links">
					<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>sets/<?=$set['id']?>/">View Set on Flickr</a>
				</p>
			</div>

			<? else: ?>
			
			<div class="image">
				<a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/<?=$photoset['id']?>/">
					<img src="<?=$fp->flickr->buildPhotoUrl($photoset, 'small')?>" width="180" alt="<?=$photoset['title']?>" title="<?=$photoset['title']?>" />
				</a>
			</div>
			<div class="info">

				<h2><a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/<?=$photoset['id']?>/">
					<?=$photoset['title']?>
				</a></h2>
				
				<p class="count">
					<?php // Fudge the numbers a little bit
					if (($fp->config['privacy_filter'] == $fp->privacy_filter['public']) or ($fp->config['privacy_filter'] == $fp->privacy_filter['private'])) {
						echo $photoset['photos'].' photos, '.$photoset['videos'].' videos';
					}
					else {
						echo 'About '.Utilities::roundToNearest($photoset['photos']).' photos, '.Utilities::roundToNearest($photoset['videos']).' videos';
					} ?>
				</p>

				<p class="links">
					<a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/<?=$photoset['id']?>/">View Set</a>
					<span class="pipe">|</span>
					<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>sets/<?=$photoset['id']?>/">View Set on Flickr</a>
				</p>

				<p class="description">
					<?=str_replace("\n", '<br />', $photoset['description'])?>
				</p>

			</div>	
			<fb:share-button class="meta">
				<meta name="title" content="Flickr Photoset: <?=$photoset['title']?>" />
				<meta name="description" content="<?=htmlentities($photoset['description'])?>" />
				<link rel="image_src" href="<?=$fp->flickr->buildPhotoUrl($photoset, 'thumbnail')?>" />
				<link rel="target_url" href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/<?=$photoset['id']?>/" />
			</fb:share-button>
			
			<? endif;?>
			
		</div>

		<?php endforeach; ?>
		
	</div>
	
	<div id="photo_footer_bar" class="bar clearfix footer_bar">
		<?=Pager::getPager($fp->page, $page_count, $fp->canvas_url.'/photos/'.$fp->uid.'/collections/'.$collection['id'].'/')?>
	</div>
	
<?php } ?>

<?=$fp->echo_footer(true)?>